def max_profit(prices):
    if len(prices) <= 1:
        return 0
    res = 0
    bottom = prices[0]
    for p in prices[1:]:
        res = max(res, p - bottom)
        bottom = min(p, bottom)
    return res
